-- holy_revive
-- created by panyl
-- 技能: 神圣重生，下次死亡自动复活，并恢复最大生命值的百分比= 0.7% * （魔力 + 10）（最高50%）。

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound;
        elseif para[3] > 0 then
            effectRound = round + para[3];
        end

        -- 最大效果
        local max = para[2];

        -- 威力
        local force = para[1];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        force = math.modf(force * (addon_rate + 1000)/1000);

        -- 魔力
        local magic = source:getMagic();

        -- 效果
        local v = math.modf(force * (magic + 15) / 100);
        v = math.min(max, v);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        local condition = {
            ["recover"] = v,
            ["end_round"] = effectRound,
        };

        -- 状态
        CombatStatusM.applyStatus(target, "holy_revive", condition);

        -- 已经复活字段去掉
        target.hasRevived = false;

        -- 施放技能增加装备经验
        local prop = PropM.combine(source, "skill_add_exp", skillId);
        if prop[3] > 0 then
            PropM.trigger(source, prop[1], prop[2]);
        end

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 最大效果
        local max = para[2];

        -- 威力
        local base = para[1];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 魔力
        local magic = source:getMagic();
        local maxHp = source:getMaxHp();

        -- 效果，百分比，有上限
        local vbase = math.modf(base * (magic + 15) / 100);
        local vforce = math.modf(force * (magic + 15) / 100);
        vbase = math.min(max, vbase);
        vforce = math.min(max, vforce);
        local recoverBase = math.modf(vbase * maxHp / 1000);
        local recoverForce = math.modf(vforce * maxHp / 1000);

        desc = string.gsub(desc, "{holy_revive}", recoverForce);

        return desc;
    end,
};
